//
//  SHComposePictureView.swift
//  SHWeibo01
//
//  Created by 卫平玉 on 16/7/27.
//  Copyright © 2016年 卫平玉. All rights reserved.
//

import UIKit

private let SHComposePictureViewCellId = "SHComposePictureViewCellId"
class SHComposePictureView: UICollectionView {
    var closure:(()->())?
    var imageList: [UIImage] = [UIImage]()

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加图片
    func addImages(image: UIImage){
        
        imageList.append(image)
        
        hidden = false
        
        //刷新数据
        reloadData()
    }
    
    
    func setupUI(){
        
        backgroundColor = UIColor.whiteColor()
        
        let cellMargin: CGFloat = 5
        let cellWH = (SCREENW - 20 - cellMargin*2)/3
        
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        layout.itemSize = CGSize(width: cellWH, height: cellWH)
        
        layout.minimumInteritemSpacing = cellMargin
        layout.minimumLineSpacing = cellMargin
     
        dataSource = self
        delegate = self
        
        registerClass(SHComposePictureViewCell.self, forCellWithReuseIdentifier: SHComposePictureViewCellId)
    }
    

}

//代理方法和数据源方法

extension SHComposePictureView: UICollectionViewDataSource,UICollectionViewDelegate{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        let count = imageList.count
        
        if count == 0 || count == 9{
            return count
        }else{
            
            return imageList.count + 1
        }
        
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(SHComposePictureViewCellId, forIndexPath: indexPath) as! SHComposePictureViewCell
        if indexPath.item == imageList.count{
            cell.image = nil
        }else{
            cell.image = imageList[indexPath.item]
        }
        
        cell.closure = {[weak self] in
            
            self?.imageList.removeAtIndex(indexPath.item)
            
            if self!.imageList.count == 0{
                self?.hidden = true
            }
            
            self!.reloadData()
        }
        

        return cell
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        // 取消…的候选资格:deselect
        collectionView.deselectItemAtIndexPath(indexPath, animated: true)
        if indexPath.item == imageList.count{
            
            closure?()
        }
    }
}




//自定义cell
class SHComposePictureViewCell: UICollectionViewCell{
    
    var closure:(()->())?
     // 定义一个属性供外界赋值
    var image: UIImage?{
        
        didSet{
            
            if image == nil {
                
                bgImageView.image = UIImage(named: "compose_pic_add")
                bgImageView.highlightedImage = UIImage(named: "compose_pic_add_highlighted")
                
                deleteButton.hidden = true
            }else{
                
                bgImageView.image = image
                
                bgImageView.highlightedImage = nil
                
                deleteButton.hidden = false
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func buttonClick(){
       closure?()
    }
    
    func setupUI(){
        contentView.addSubview(bgImageView)
        contentView.addSubview(deleteButton)
        
        //添加约束
        
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        
        deleteButton.snp_makeConstraints { (make) -> Void in
            make.top.trailing.equalTo(contentView)
        }

        
    }
    
    
    
    private lazy var bgImageView: UIImageView = UIImageView()
    
    private lazy var deleteButton: UIButton = {
        
        let btn = UIButton()
        
        btn.setImage(UIImage(named: "compose_photo_close"), forState: UIControlState.Normal)
        btn.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
}
